var tools = require('../../../libs/tools');
var config = require(tools.rootPath + 'config');
var { connection, pool, model } = require(tools.rootPath + 'libs/model');

var controller = require(tools.rootPath + 'libs/controller.js');

var captcha = require('hahoo-captcha');
var gm = require('gm').subClass({ imageMagick: true });
// const { promisify } = require("es6-promisify");
class index extends controller {
    constructor() {
        super();
    }

    index(req, res) {
        console.log('controller: home/index/index');
        console.log('ttt', req.url);
        res.end('controller: home/index/index');
    }

    async test() {
        console.log('controller: home/index/test');
        let data = await model.select("select * from test");
        console.log(data);
        console.log(model.get_users());
        res.end('controller: home/index/test');
    }
    _p() {
        res.end('_p');
    }
    tip() {
        res.end(this.msg(1, 'success', '/home/index/index'));
    }
    async captcha(req, res) {
        let data = await captcha.default.toBuffer();
        //console.log(data);
        req.session.CAPTCHA = data.text;
        res.header('Content-type', 'image/png');
        res.end(data.buffer);
    }
    async resize(req, res) {
        // gm(tools.publicPath + 'imgs/Seoul15.jpg')
        //     .resize(240, 240)
        //     .noProfile()
        //     .write(tools.publicPath + 'imgs/Seoul15_240_240.jpg', function (err) {
        //         console.log(err);
        //         if (!err) console.log('done');
        //     });
        function gm_resize(src, dst) {
            return new Promise((resolve, reject) => {
                gm(src).resize(240, 240).noProfile().write(dst, function (err) {
                    if (err) {
                        resolve(false);
                    } else {
                        resolve(true);
                    }
                });
            });
        }
        let r = await gm_resize(tools.publicPath + 'imgs/Seoul15.jpg', tools.publicPath + 'imgs/Seoul15_240_240.jpg');
        console.log(r);
        res.end('ok');
    }
}

module.exports = index;